<template>
    <div class="order-report">
        <div class="report-table-top-title">库存查询</div>
        <div class="report-table-form1">
            <div class="report-table-form1-line">
                <div
                    class="report-table-form1-item word samll"
                    style="width: 50px"
                >
                    代理商:
                </div>
                <div class="report-table-form1-item" style="width: 180px">
                    <el-select
                        v-model="value1"
                        filterable
                        placeholder="请选择"
                        size="small"
                    >
                        <el-option label="全部" value=""></el-option>
                        <el-option
                            v-for="item in options1"
                            :key="item.id"
                            :label="item.agentName"
                            :value="item.id"
                        ></el-option>
                    </el-select>
                </div>
                <!-- <div class="report-table-form1-item" style="width: 50px"></div> -->
                <!--<div class="report-table-form1-item" style="width: 100px">-->
                <!--</div>-->
                <el-button type="primary" size="small" @click="exportRport"
                    >导出Excel</el-button
                >
            </div>
        </div>
        <div class="page-padding-gray-small"></div>
        <div class="report-table-wrapper">
            <el-table
                :header-cell-style="headerCellStyle"
                size="small"
                :cell-style="cellStyle"
                :data="showList"
                border
                style="width: 100%"
            >
                <el-table-column
                    prop="agentName"
                    label="代理商名称"
                    width="180"
                ></el-table-column>
                <!-- <el-table-column prop="registedSoftwareNum" label="云管已使用点数"></el-table-column>
                <el-table-column prop="unregistedSoftwareNum" label="云管未使用点数"></el-table-column>
                <el-table-column prop="useCloudsNum" label="云管云存储已使用数量"></el-table-column>
                <el-table-column prop="unUseCloudsNum" label="云管云存储未使用数量"></el-table-column> -->
                <el-table-column
                    prop="yjSoftwarePackage"
                    label="利云酒吧软件总套数"
                    v-if="$includesType(2)"
                ></el-table-column>
                <el-table-column
                    v-if="$includesType(2)"
                    prop="yjUnUseSoftwarePackage"
                    label="利云酒吧软件剩余套数"
                ></el-table-column>
                <!--  <el-table-column prop="yjYunSoftwarePackage" label="娱加云软件总套数"></el-table-column>
                <el-table-column prop="yjYunUnUseSoftwarePackage" label="娱加云软件剩余套数"></el-table-column> -->
                <el-table-column
                    prop="ysSoftwareInventory"
                    v-if="$includesType(4)"
                    label="养生软件总套数"
                ></el-table-column>
                <el-table-column
                    prop="ysUnUseSoftwareInventory"
                    v-if="$includesType(4)"
                    label="养生软件剩余总套数"
                ></el-table-column>
                <el-table-column
                    prop="activeTerminalNum"
                    label="设备已使用 (台)"
                ></el-table-column>
                <el-table-column
                    prop="notActiveTerminalNum"
                    label="设备未使用 (台)"
                ></el-table-column>
                <el-table-column label="操作" width="75">
                    <template slot-scope="scope">
                        <el-button
                            v-if="scope.row.agentName !== '合计'"
                            size="mini"
                            type="primary"
                            @click="toDetail(scope.$index, scope.row)"
                            >详情</el-button
                        >
                    </template>
                </el-table-column>
            </el-table>
        </div>

        <div class="report-table-block">
            <div class="report-table-block-in">
                <el-pagination
                    background
                    :current-page.sync="position"
                    @current-change="handleCurrentChange"
                    :page-size="10"
                    layout="prev, pager, next, total"
                    :total="totalNum"
                ></el-pagination>
            </div>
        </div>
        <div class="page-padding"></div>
    </div>
</template>

<script type="text/ecmascript-6">
import fileDownload from 'js-file-download'
import axios from 'axios'
export default {
    data: function () {
        return {
            totalNum: 0,
            position: 1,

            options1: [],
            value1: '',

            showList: []
        }
    },
    mounted () {
    },
    created () {
        this.getAllAgent()
        this.getPage()
    },
    watch: {
        value1 () {
            this.position = 1
            this.getPage()
        },
        position () {
            this.getPage()
        }
    },
    components: {
    },
    props: {
        underLine: null
    },
    computed: {
        manageArr () {
            console.log(this.$store.state.manageArr)
            return this.$store.state.manageArr
        },
        roleId () {
            return this.$store.state.roleId
        },
        manageRoleType () {
            return this.$store.state.manageRoleType
        }
    },
    methods: {
        cellStyle ({ row, column, rowIndex, columnIndex }) {
            if (row.agentName === '合计') {
                return {
                    background: 'rgb(240,240,240)'
                }
            }
        },

        getAllAgent () {
            this.$http.get(this.$store.state.IP + '/systemUserManage/getAgents', {
                data: {
                }
            },
                {
                    'emulateJSON': false
                }
            ).then(response => {
                console.log('-------------------------------------------')
                response = response.body
                console.log(response)
                if (response.ret === '0') {
                    console.log('调用获取所有代理商成功')
                    this.options1 = response.res
                }
                if (response.ret !== '0') {
                    console.log('调用获取所有代理商失败')
                }
                console.log('******************************************')
            }, response => {
                console.log('通讯失败')
            })
        },

        toDetail (index, row) {
            this.$store.dispatch('setSM', 1)
            this.$store.dispatch('setSMAI', row.agentId)
            this.$store.dispatch('setSMAN', row.agentName)
        },

        exportRport () {
            let that = this
            axios.get(this.$store.state.IP + '/system/stock/exportStockExcel' +
                '?agentId=' + this.value1,
                {
                    responseType: 'blob',
                    headers: that.axiosDownloadHeaders()
                }
            ).then(function (response) {
                console.log(response)
                if (response.headers['content-type'] === 'application/json;charset=UTF-8') {
                    console.log('出错')
                    return
                }
                let fileName = decodeURIComponent(response.headers['content-disposition'])
                fileName = fileName.substring(20)
                if (response.headers['content-type'] === 'application/x-msdownload;charset=UTF-8') {
                    fileDownload(response.data, fileName)
                }
            }).catch(function (error) {
                alert(error)
            })
        },

        // 换页方法
        handleCurrentChange (val) {
            this.position = val
        },
        getPage () {
            this.$http.post(this.$store.state.IP + '/system/stock/getStockStatistics', {
                data: {
                    agentId: this.value1,
                    page: this.position - 1, // 页数
                    limit: 10 // 一页显示数量
                }
            },
                {
                    'emulateJSON': false
                }
            ).then(response => {
                console.log('-------------------------------------------')
                response = response.body
                console.log(response)
                if (response.ret === '0') {
                    console.log('调用库存列表成功')
                    console.log(response.data)
                    this.showList = response.data.list
                    this.totalNum = response.data.total
                }
                if (response.ret !== '0') {
                    console.log('调用库存列表失败')
                }
                console.log('******************************************')
            }, response => {
                console.log('通讯失败')
            })
        }
    }
}
</script>

<style lang="stylus" rel="stylesheet/stylus">
.order-report
    width: 100%
    background: #fff
    min-height: calc(100vh - 65px)

    .report-table-top-title
        width: 100%
        height: 64px
        line-height: 64px
        color: #000
        font-size: 18px
        background: #f1f0f5

    .report-table-form1
        width: 100%
        min-height: 40px
        padding: 16px 0 0 22px
        box-sizing: border-box
        background: #fff
        overflow: hidden

        .report-table-form1-line
            min-height: 32px
            margin-bottom: 16px

            .report-table-form1-item
                display: inline-block
                vertical-align: top
                height: 32px
                margin-right: 15px
                line-height: 32px

                &.word
                    margin-right: 0

                &.samll
                    font-size: 15px
                    text-align: left

                &.big
                    margin-right: 100px

    .report-table-block
        width: 100%
        padding-top: 22px
        height: 50px
        background: #fff
        box-sizing: border-box

        .report-table-block-in
            float: right
            padding-right: 17px

    .page-padding-gray-small
        width: 100%
        height: 22px
        background: #f1f0f5

    .page-padding
        width: 100%
        height: 20px
        background: #fff

    .report-table-total
        width: 100%
        font-size: 16px
        color: #409EFF
        padding: 22px
        margin-top: 22px
        box-sizing: border-box
        background: #fff
</style>
